package com.lnsystem.service.impl;


import java.util.ArrayList;
import java.util.List;

import com.lnsystem.model.MovingAverage;
import com.lnsystem.model.StockDailyData;

public class SMALogicTester {

	
	
	public static void main(String[] args) {
		
		double sma = 0;
		int period = 50;
		double runningSum = 0;
		double overFlowValue = 0;
		
		
		
		QuoteServiceTKImpl qSvcImpl = new QuoteServiceTKImpl();
		ScheduledTaskImpl sImpl = new  ScheduledTaskImpl();
		
		List<StockDailyData> dataList = new ArrayList<StockDailyData>();
		dataList = qSvcImpl.getHistoricalQuotes("IBM", "2013-06-10", "2014-06-10", "daily");
		
		sImpl.calculateSMA(dataList, period);
		for(StockDailyData stockDailyData : dataList) {
			System.out.println("SMA on ["+stockDailyData.getDate()+"] --> "+stockDailyData.getMovingAverages().get(0).getMaValue());

		}

			
		
		
		
/*		int i=0;
		for(StockDailyData stockDailyData : dataList) {
			
			if(i>0) {
				runningSum = dataList.get(i-1).getMovingAverages().get(0).getRunningSum();
			}
			if(i>period) {
				overFlowValue = dataList.get(i-period).getClose();
			}
			calculateSMA(stockDailyData, i, runningSum, overFlowValue, period );
			System.out.println("SMA on ["+stockDailyData.getDate()+"] --> "+stockDailyData.getMovingAverages().get(0).getMaValue());
		}*/
	}
    public static void calculateSMA(StockDailyData stock, int index, double runningSum, double overFlowValue,  int days) { 
    	
    	MovingAverage ma = null;
		runningSum += stock.getClose();
		
		if(index < days) {
			ma = new MovingAverage(0,days,runningSum / (index+1));

		} else {
			
			runningSum -= overFlowValue;
			 ma = new MovingAverage(0,days,runningSum / days);
			
		}
		ma.setRunningSum(runningSum);
		stock.getMovingAverages().add(ma);
		
    }
 	
}
